﻿using Api.Ali;

namespace Services;

/// <summary>
/// 上传
/// </summary>
[Authorize, ApiDescriptionSettings("上传")]
public class UploadServices : BaseServices
{
    private readonly Oss oss;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="oss"></param>
    /// <param name="orc"></param>
    public UploadServices(Oss oss)
    {
        this.oss = oss;
    }

    /// <summary>
    /// 视频
    /// </summary>
    /// <param name="storeId"></param>
    /// <returns></returns>
    [RequestSizeLimit(long.MaxValue)]
    public async Task<object> Video(int storeId)
    {
        var file = App.HttpContext.Request.Form.Files[0];

        string url = oss.Upload($"{ROLE.ToString()}/video/{storeId}", file.OpenReadStream(), file.FileName);

        return Ok(await Task.FromResult(url));
    }

    /// <summary>
    /// 图片
    /// </summary>
    /// <param name="storeId"></param>
    /// <param name="dir"></param>
    /// <returns></returns>
    public async Task<object> Image(int storeId, [FromQuery] string dir = "")
    {
        var file = App.HttpContext.Request.Form.Files[0];

        if (file.Length > 1024 * 1024 * 5)
        {
            return No("文件大于5M");
        }

        if (dir.Length == 0) dir = ROLE.ToString();


        string url = oss.Upload($"{dir}/Image/{storeId}/{DateTime.Now.ToString("yyyyMM")}", file.OpenReadStream(), file.FileName);

        return Ok(await Task.FromResult(url));
    }

    /// <summary>
    /// 客户端上传
    /// </summary>
    /// <param name="dir"></param>
    /// <returns></returns>
    public async Task<object> Image([FromQuery] string dir = "")
    {
        var file = App.HttpContext.Request.Form.Files[0];

        if (file.Length > 1024 * 1024 * 5)
        {
            return No("文件大于5M");
        }

        if (dir.Length == 0) dir = ROLE.ToString();


        string url = oss.Upload($"{dir}/Image/{DateTime.Now.ToString("yyyyMM")}", file.OpenReadStream(), file.FileName);

        return Ok(await Task.FromResult(url));
    }

    /// <summary>
    /// 管理员上传
    /// </summary>
    /// <returns></returns>
    public async Task<object> Admin()
    {
        var file = App.HttpContext.Request.Form.Files[0];

        if (file.Length > 1024 * 1024 * 5)
        {
            return No("文件大于5M");
        }

        string url = oss.Upload($"admin/{DateTime.Now.ToString("yyyyMM")}", file.OpenReadStream(), file.FileName);

        return Ok(await Task.FromResult(url));
    }

   
}
